﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "AbilitySystem/Abilities/StarrySkyProjectileSpell.h"

#include "Actor/StarrySkyProjectile.h"
#include "Interaction/CombatInterface.h"

void UStarrySkyProjectileSpell::ActivateAbility(const FGameplayAbilitySpecHandle Handle,
                                                const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo,
                                                const FGameplayEventData* TriggerEventData)
{
	Super::ActivateAbility(Handle, ActorInfo, ActivationInfo, TriggerEventData);

}

void UStarrySkyProjectileSpell::SpawnProjectile(const FVector& ProjectileTargetLocation, const FGameplayTag& SocketTag,
	bool bOverridePitch, float PitchOverride)
{
	//判断是否是服务器上调用,传入ActivationInfo的引用
	const bool bIsServer=GetAvatarActorFromActorInfo()->HasAuthority();
	if(!bIsServer)
	{
		return;
	}
	//需要使用执行蓝图方法
	const FVector SocketLocation=ICombatInterface::Execute_GetCombatSocketLocation(GetAvatarActorFromActorInfo(),SocketTag);
	//通过两个向量获取旋转
	FRotator Rotation=(ProjectileTargetLocation-SocketLocation).Rotation();
	
	/*希望抛射物和目标点在同一个平面上飞行，所以把Y轴角度归0
	* Pitch：围绕 Y 轴的旋转，控制上下倾斜的角度。正值表示向上倾斜，负值表示向下倾斜。
	* Yaw：围绕 Z 轴的旋转，控制左右旋转的角度。正值表示向右旋转，负值表示向左旋转。
	* Roll：围绕 X 轴的旋转，控制翻滚的角度。正值表示向右翻滚，负值表示向左翻滚。
	*/
	//Rotation.Pitch=0.f;	
	//延迟生成ProjectileClass角色
	FTransform SpawnTransform;
	SpawnTransform.SetLocation(SocketLocation);
	SpawnTransform.SetRotation(Rotation.Quaternion());
	
	//创建抛射物Actor
	AStarrySkyProjectile* Projectile=GetWorld()->SpawnActorDeferred<AStarrySkyProjectile>(ProjectileClass,SpawnTransform,
		GetOwningActorFromActorInfo(),Cast<APawn>(GetOwningActorFromActorInfo()),
		ESpawnActorCollisionHandlingMethod::AlwaysSpawn);
	
	Projectile->DamageEffectParams=MakeDamageEffectParamsFromClassDefaults();
	//完成生成
	Projectile->FinishSpawning(SpawnTransform);
}
